package com.wy.callable_demo;

import java.util.concurrent.*;

public class Main {
    static class FibCalc implements Callable<Long> {
        private final int n;

        FibCalc(int n) {
            this.n = n;
        }

        private long fib(int n) {
            if (n == 0 || n == 1) {
                return 1;
            }

            return fib(n - 1) + fib(n - 2);
        }

        @Override
        public Long call() throws Exception {
            return fib(n);//返回fib数的值
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService service = Executors.newSingleThreadExecutor();
        FibCalc task = new FibCalc(45);//主线程发布了一个任务
        //futur:未来，期望；未来会获取到这个值，但可能因为计算获取到的慢
        Future<Long> future = service.submit(task);//提交任务快
        Long r = future.get();  // 这一步实际上是在等任务计算完成，所以，时间可能需要很久
        System.out.println(r);
    }
}

